﻿using UnityEngine;

namespace UnityX
{
    [DisallowMultipleComponent]
    [RequireComponent(typeof(RectTransform))]
    [AddComponentMenu("Animation/UI Slide In")]
    public class UISlideIn : AnimationBehaviour
    {
        public Vector2 slideSize = Vector2.zero;
        public MoveValueType slideSizeType = MoveValueType.基于父亲滑出大小;

        private Vector2 m_BeginValue;
        private Vector2 m_EndValue;
        private RectTransform m_RectTransform;
        protected override void Begin()
        {
            if (m_RectTransform == null)
                m_RectTransform = GetComponent<RectTransform>();
            m_EndValue = m_RectTransform.anchoredPosition;
            m_BeginValue = GetSlidePosition(m_RectTransform, slideSizeType, slideSize);
            m_RectTransform.anchoredPosition = m_BeginValue;
        }
        protected override void End()
        {
            m_RectTransform.anchoredPosition = m_EndValue;
        }
        protected override void Set(float t)
        {
            m_RectTransform.anchoredPosition = Vector2.Lerp(m_BeginValue, m_EndValue, t);
        }
        private Vector2 GetSlidePosition(RectTransform rectTransform, MoveValueType slideType, Vector2 slide)
        {
            switch (slideType)
            {
                case MoveValueType.固定位置:
                    return slide;
                case MoveValueType.固定偏移:
                    return rectTransform.anchoredPosition + slide;
                case MoveValueType.基于自己大小偏移:
                    return rectTransform.anchoredPosition + rectTransform.rect.size * slide;
                case MoveValueType.基于父亲大小偏移:
                    {
                        RectTransform parentRectTransform = rectTransform.parent as RectTransform;
                        if (parentRectTransform != null)
                            return rectTransform.anchoredPosition + parentRectTransform.rect.size * slide;
                        else
                            return rectTransform.anchoredPosition + new Vector2(Screen.width, Screen.height) * slide;
                    }
                case MoveValueType.基于父亲滑出大小:
                    {
                        Rect rect = rectTransform.ExGetLocalRect();
                        RectTransform parentRectTransform = rectTransform.parent as RectTransform;
                        if (parentRectTransform != null)
                        {
                            return rectTransform.anchoredPosition + new Vector2(
                                slide.x < 0 ? rect.xMax : (parentRectTransform.rect.width - rect.xMin),
                                slide.y < 0 ? rect.yMax : (parentRectTransform.rect.height - rect.yMin)) * slide;
                        }
                        else
                        {
                            return rectTransform.anchoredPosition + new Vector2(
                                slide.x < 0 ? rect.xMax : (Screen.width - rect.xMin),
                                slide.y < 0 ? rect.yMax : (Screen.height - rect.yMin)) * slide;
                        }
                    }
                case MoveValueType.基于屏幕滑出大小:
                    {
                        Rect rect = rectTransform.ExGetWorldRect();
                        return rectTransform.anchoredPosition + new Vector2(
                            slide.x < 0 ? rect.xMax : (Screen.width - rect.xMin),
                            slide.y < 0 ? rect.yMax : (Screen.height - rect.yMin)) * slide;
                    }
            }
            return slide;
        }
    }
}